$(function () {
    let id = sessionStorage.getItem("id");
    findById(id);
})

function findById(id) {
    $.ajax({
        url: '/back/user/findById',
        data: {id: id},
        dataType: "json",
        type: 'get',
        async: false,
        success: function (data) {
            // console.log(data,"kankan");
            $("#username").val(data.data.username);
        }
    })
}

layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        //自定义验证规则
        form.verify({
            new_password: [/(.+){6,12}$/, '密码必须6到12位'],
            password: function (value) {
                if ($('#new_password').val() != $('#password').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });
        //监听提交
        form.on('submit(update)',
            function (data) {
                data = data.field;
                let uid = sessionStorage.getItem("id");
                console.log(uid, "uid");
                console.log(data.old_password, "表单输入的旧密码");
                console.log(data.new_password, "表单输入的新密码");
                $.ajax({
                    url: '/back/user/update',
                    data: {uid: uid, old_password: data.old_password, new_password: data.new_password},
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.msg == 'success') {
                            layer.alert("成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else if (data.msg == 'error-password') {
                            layer.msg("密码错误");
                        }else {
                            layer.msg("修改失败");
                        }
                    }

                });
                return false;
            });

    });